package cn.ycc1.boot3redisson.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * @author ycc
 * @date 2024/7/31
 */
@Configuration
@Slf4j
public class MyRedissonKeyGenerator {

    /**
     * 默认的key生成器
     */
    public static final String myKeyGeneratorByMethodName = "myKeyGeneratorByMethodName";

    /**
     * 类全名+方法名
     * @return
     */
    @Bean(myKeyGeneratorByMethodName)
    public KeyGenerator myKeyGeneratorByMethodName() {
        KeyGenerator keyGenerator = new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                String className = target.getClass().getName();
                String methodName = method.getName();
                StringBuffer cacheKey = new StringBuffer();
                cacheKey.append(className).append(".").append(methodName);
                return cacheKey.toString();
            }
        };
        return keyGenerator;
    }
}
